package com.zggk.lzz.sjcx.done;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zggk.lzz.R;
import com.zggk.lzz.adapter.LGRecycleViewAdapter;
import com.zggk.lzz.adapter.LGViewHolder;
import com.zggk.lzz.mvp.MVPBaseFragment;
import com.zggk.lzz.sjcx.SjcxBean;
import com.zggk.lzz.sjxq.SjxqActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class DoneFragment extends MVPBaseFragment<DoneContract.View, DonePresenter> implements DoneContract.View {
    @Bind(R.id.sjcx_num)
    TextView sjcxNum;
    @Bind(R.id.te_lay)
    LinearLayout teLay;
    @Bind(R.id.recycle_view)
    RecyclerView recycleView;
    @Bind(R.id.wt_te)
    LinearLayout wtTe;
    private LGRecycleViewAdapter<SjcxBean.DataBean.RecordsBean> adapter;
    private ArrayList<SjcxBean.DataBean.RecordsBean> ListDatas = new ArrayList<>();
    private String orgName = "";
    private String etype = "";
    private String startDate = "";
    private String endDate = "";

    @Override
    public void onRequestError(String msg) {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.done_fragment, container, false);
        ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void initData() {
        mPresenter.getData(orgName, etype, startDate, endDate, getActivity());
    }

    private void initView() {
        recycleView.setHasFixedSize(true);
        recycleView.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(manager);
        adapter = new LGRecycleViewAdapter<SjcxBean.DataBean.RecordsBean>(ListDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_sjcx;
            }

            @Override
            public void convert(LGViewHolder holder, SjcxBean.DataBean.RecordsBean zCDBean, int position) {
                holder.setText(R.id.name_te, zCDBean.getEtype());
                holder.setText(R.id.scmc_te, zCDBean.getCreateUserName());
                holder.setText(R.id.bhlx_te, zCDBean.getEtype());
                holder.setText(R.id.sj_te, getDateTimeFromMillisecond(zCDBean.getCreateTime()));
            }
        };
        recycleView.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.re_lay, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                Intent intent = new Intent(getActivity(), SjxqActivity.class);
                intent.putExtra("id", ListDatas.get(position).getId());
                startActivity(intent);
            }
        });
    }

    public static String getDateTimeFromMillisecond(String millisecond) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
        Date date = new Date();
        try {
            date = sdf.parse(millisecond);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String sDate = sdf2.format(date);
        return sDate;
    }

    @Override
    public void getDatas(SjcxBean.DataBean dataBean) {
        ListDatas.clear();
        ListDatas.addAll(dataBean.getRecords());
        adapter.notifyDataSetChanged();
        sjcxNum.setText("共上报" + ListDatas.size() + "个问题");
    }

    /**
     * 刷新数据
     */
    public void refreshDataMethod(String orgName, String etype, String startDate, String endDate) {
        mPresenter.getData(orgName, etype, startDate, endDate, getActivity());
    }

}

